﻿using System;
using System.Windows.Input;
using IronCow;
using Milkify.Data;
using Milkify.Input;
using Milkify.Interfaces;

namespace Milkify.Controllers
{
    class HomeController : Controller
    {
        private IRefreshableView mView;

        public HomeController(ControllerContext context, Rtm rtm, IViewContainer viewContainer, IViewFactory viewFactory)
            : base(context, rtm, viewContainer)
        {
            mView = (IRefreshableView)viewFactory.Create("Home");
            mView.Loaded += new System.Windows.RoutedEventHandler(View_Loaded);
            mView.Unloaded += new System.Windows.RoutedEventHandler(View_Unloaded);
            mView.NeedsRefresh = true;
        }

        public override void Execute()
        {
            // Create commands.
            ICommand navigateToTaskListsCommand = new DecoupledCommand(NavigateToTaskLists);
            ICommand navigateToTaskListCommand = new DecoupledCommand<TaskListViewData>(NavigateToTaskList);
            ICommand navigateToWeekViewCommand = new DecoupledCommand(NavigateToWeekView);
            ICommand navigateToTagsCommand = new DecoupledCommand(NavigateToTags);

            // Create view data.
            var viewData = new HomeViewData()
            {
                Today = new TaskListViewData("Today", "status:incomplete AND dueBefore:tomorrow"),
                Tomorrow = new TaskListViewData("Tomorrow", "status:incomplete AND due:tomorrow"),
                NavigateToTaskLists = navigateToTaskListsCommand,
                NavigateToTaskList = navigateToTaskListCommand,
                NavigateToWeekView = navigateToWeekViewCommand,
                NavigateToTags = navigateToTagsCommand
            };
            mView.ViewData = viewData;

            // Navigate to our view.
            ViewContainer.NavigateToView(mView);
        }

        private void View_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            // Optionally refresh the UI.
            if (mView.NeedsRefresh)
            {
                RefreshLists();
                mView.NeedsRefresh = false;
            }
        }

        private void View_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
        }

        private void RefreshLists()
        {
            HomeViewData viewData = mView.ViewData as HomeViewData;
            if (viewData == null)
                throw new InvalidOperationException("The view data for the home view hasn't been set yet.");

            viewData.Today.RefreshAsync(Rtm);
            viewData.Tomorrow.RefreshAsync(Rtm);
        }

        private void NavigateToTaskLists()
        {
            Redirect("TaskListList", "NavigateToTaskLists");
        }

        private void NavigateToTaskList(TaskListViewData taskList)
        {
            Redirect("TaskList", "NavigateToTaskList", taskList);
        }

        private void NavigateToWeekView()
        {
            Redirect("Week", "NavigateToWeekView");
        }

        private void NavigateToTags()
        {
            Redirect("TagList", "NavigateToTags");
        }

        private void NavigateToLocations()
        {
            //TODO
        }

        private void Refresh(object sender, ExecutedRoutedEventArgs e)
        {
            RefreshLists();
            e.Handled = true;
        }
    }
}
